package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.FamilayHistoryResponse;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.net.SickHistoryApi;
import com.right.health.app.ui.medical.adapter.FamilyMedicalAdapter;
import com.right.health.app.widget.ProgressLayout;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_FAMILY_HISTORY;

/*
 * Created by hopeliao on 2019-10-01
 * 家族史
 *
 */
public class FamilyMedicalActivity extends BaseActivity implements OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private FamilyMedicalAdapter mAdapter;

    private SickHistoryApi mSickHistoryApi;

    private SwipeListRefreshLayout mRefreshView;

    private ProgressLayout mProgressLayout;

    private int mPageNo;

    private List<FamilayHistoryResponse.FamilyHistoryInfo> mDataSource = new ArrayList<>();

    private List<FamilyResponse.FamilyInfo> mFamilyList;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.family_medical);
    }

    @Override
    public void findView() {
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);
        mProgressLayout = findViewById(R.id.pl);
        ListView listViewEx = (ListView) findViewById(R.id.listview);
        mAdapter = new FamilyMedicalAdapter(this);
        listViewEx.setAdapter(mAdapter);

        findViewById(R.id.ib_add).setOnClickListener(v -> startIntent(FamilyMedicalAddActivity.class));
    }


    @Override
    public void initialize() {
        mFamilyList = DataCommon.getFamilyRelationship();

        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);

        onReceiveBroadcast(REFRESH_FAMILY_HISTORY, null);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == REFRESH_FAMILY_HISTORY) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
            mProgressLayout.hide();
        } else {
            if (response.targetData instanceof FamilayHistoryResponse) {
                FamilayHistoryResponse familayHistoryResponse = response.cast(FamilayHistoryResponse.class);

                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }

                if (familayHistoryResponse.page != null) {
                    mRefreshView.setCanLoadMore(!familayHistoryResponse.page.lastPage);
                }
                if (!CollectionUtil.isEmpty(familayHistoryResponse.familyhistorys)) {
                    for (FamilayHistoryResponse.FamilyHistoryInfo info : familayHistoryResponse.familyhistorys) {
                        for (FamilyResponse.FamilyInfo familyInfo : mFamilyList) {
                            if (info.diseasetype.equals(familyInfo.membership)) {
                                info.diseasetype = familyInfo.shipName;
                                break;
                            }
                        }
                    }
                    mDataSource.addAll(familayHistoryResponse.familyhistorys);
                    mAdapter.setDataSource(mDataSource);
                    mProgressLayout.hide();
                } else {
                    mProgressLayout.showInfo("暂无数据");
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;

        mSickHistoryApi.addRequestCode(HTTP_CODE_REFRESH);
        mSickHistoryApi.getFamilyHistoryList(App.getInstance().getCurrentMember().id, mPageNo);
    }

    @Override
    public void onLastItemVisible() {
        mPageNo += 1;
        mSickHistoryApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mSickHistoryApi.getFamilyHistoryList(App.getInstance().getCurrentMember().id, mPageNo);
    }
}
